package com.amap.map2d.demo.overlay;

import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;

import com.amap.api.maps2d.AMap;
import com.amap.api.maps2d.CameraUpdate;
import com.amap.api.maps2d.CameraUpdateFactory;
import com.amap.api.maps2d.MapView;
import com.amap.api.maps2d.model.BitmapDescriptorFactory;
import com.amap.api.maps2d.model.CameraPosition;
import com.amap.api.maps2d.model.LatLng;
import com.amap.api.maps2d.model.MarkerOptions;
import com.amap.map2d.demo.R;
import com.amap.map2d.demo.util.DBScan;
import com.amap.map2d.demo.util.Point;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;

/**
 * AMapV2地图中简单介绍一些Marker的用法.
 */
public class HotMarkerActivity extends Activity {
    private MarkerOptions markerOption;
    private AMap aMap;
    private MapView mapView;
    private Map<Integer, Point> sets = new HashMap<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.marker_activity);
        /*
         * 设置离线地图存储目录，在下载离线地图或初始化地图设置; 使用过程中可自行设置, 若自行设置了离线地图存储的路径，
         * 则需要在离线地图下载和使用地图页面都进行路径设置
         */
        // Demo中为了其他界面可以使用下载的离线地图，使用默认位置存储，屏蔽了自定义设置
        // MapsInitializer.sdcardDir =OffLineMapUtils.getSdCacheDir(this);
        mapView = (MapView) findViewById(R.id.map);
        mapView.onCreate(savedInstanceState); // 此方法必须重写
        init();
    }

    /**
     * 初始化AMap对象
     */
    private void init() {
        if (aMap == null) {
            aMap = mapView.getMap();
            addMarkersToMap();// 往地图上添加marker
        }
    }

    /**
     * 方法必须重写
     */
    @Override
    protected void onResume() {
        super.onResume();
        mapView.onResume();
    }

    /**
     * 方法必须重写
     */
    @Override
    protected void onPause() {
        super.onPause();
        mapView.onPause();
    }

    /**
     * 方法必须重写
     */
    @Override
    protected void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        mapView.onSaveInstanceState(outState);
    }

    /**
     * 方法必须重写
     */
    @Override
    protected void onDestroy() {
        super.onDestroy();
        mapView.onDestroy();
    }

    /**
     * 在地图上添加marker
     */
    private void addMarkersToMap() {
        Toast.makeText(getApplicationContext(), "算法处理中", Toast.LENGTH_LONG).show();
        new Thread() {
            @Override
            public void run() {
                Set<Point> pointSet = DBScan.getData(getApplicationContext());
                Iterator<Point> iterator = pointSet.iterator();
                while (iterator.hasNext()) {
                    Point point = iterator.next();
                    sets.put(point.getCluster(), point);
                }
                Message message = new Message();
                hand.sendMessage(message);

            }
        }.start();
    }


    Handler hand = new Handler() {
        @Override
        public void handleMessage(Message msg) {

            try {


                double a = 0, b = 0;
                Iterator<Integer> iterator1 = sets.keySet().iterator();
                while (iterator1.hasNext()) {
                    Point point = sets.get(iterator1.next());
                    markerOption = new MarkerOptions().icon(BitmapDescriptorFactory
                            .fromResource(R.drawable.zb))
                            .position(new LatLng(point.getValue()[0], point.getValue()[1]))
                            .draggable(true);
                    aMap.addMarker(markerOption);
                    a = point.getValue()[0];
                    b = point.getValue()[1];
                }

                CameraPosition cameraPosition = new CameraPosition(new LatLng(a, b), 8, 0, 30);
                CameraUpdate cameraUpdate = CameraUpdateFactory.newCameraPosition(cameraPosition);
                aMap.moveCamera(cameraUpdate);//地图移向指定区域

            } catch (Exception e) {
                Toast.makeText(getApplicationContext(), "无数据，或者数据异常", Toast.LENGTH_LONG).show();
            }

        }
    };

}
